﻿using System;
using System.Collections.Generic;
using NHibernate.Mapping.ByCode;
using Xenta.Enums;

namespace Xenta.Entities
{
    /// <summary>
    /// Represents the language entity.
    /// </summary>
    /// <remarks><pre>
    /// The entity regarding the disclosure level.
    /// 
    /// + Entire
    ///  + Extended
    ///   + Base
    ///    | - Name
    ///    | - Flags
    ///    | - UpdatedOn
    ///    | - CreatedOn
    ///    + Keys
    ///     | - Code
    ///     + Primary
    ///      | - EntityID
    /// 
    /// Note: upper level includes lower level.
    /// </pre></remarks>
    public class LanguageEntity : AuditableEntity
    {
        #region Properties

        /// <summary>
        /// Gets or sets the code.
        /// </summary>
        /// <remarks>
        /// ISO 639-1
        /// </remarks>
        public virtual string Code
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        public virtual string Name
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        public virtual LanguageFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override void Disclose(IDictionary<String, Object> obj,
            DisclosureLevel level,
            IDictionary<String, String> options)
        {
            base.Disclose(obj, level, options);
            if(level >= DisclosureLevel.Keys)
                obj["Code"] = Code;
            if(level >= DisclosureLevel.Base)
            {
                obj["Name"] = Name;
                obj["Flags"] = Flags.ToString();
            }
        }

        #endregion
    }

    /// <summary>
    /// The entity NHibernate mapper.
    /// </summary>
    public class LanguageMapping : EntityMapping<LanguageEntity>
    {
        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public LanguageMapping()
        {
            Table("fwk_Languages");
            Id(x => x.EntityID, m =>
            {
                m.Column("LanguageID");
                m.Generator(Generators.Identity);
            });
            Property(x => x.Code, m =>
            {
                m.NotNullable(true);
                m.Length(2);
                m.Unique(true);
                m.Index("IX_fwk_Languages");
                m.Column(c =>
                {
                    c.SqlType("nchar(2)");
                });
            });
            Property(x => x.Name, m =>
            {
                m.NotNullable(true);
                m.Length(100);
                m.Column(c =>
                {
                    c.SqlType("nvarchar(100)");
                });
            });
            Property(x => x.Flags, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.CreatedOn, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.UpdatedOn, m =>
            {
                m.NotNullable(true);
            });
            Cache(m =>
            {
                m.Usage(CacheUsage.ReadWrite);
            });
        }
    }
}
